package com.baizhi.controller;

import com.baizhi.entity.Shebei;
import com.baizhi.service.ShebeiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("shebei")
public class ShebeiController {
    private static final Logger log = LoggerFactory.getLogger(ShebeiController.class);
    private ShebeiService shebeiService;

    @Autowired

    public ShebeiController(ShebeiService shebeiService) {
        this.shebeiService = shebeiService;
    }





    /**
     * 展示所有设备的信息
     *
     * @param model
     * @return
     */
    @RequestMapping("lists")
    public String lists(Shebei shebei, Model model) {
        shebeiService.lists(shebei, model);
        return "emplist";
    }

    /**
     * 根据IP androidId字段去查询设备信息
     *
     * @param androidId
     * @param
     * @param model
     * @return
     */
    @RequestMapping("findById")
    public String findById(Integer androidId, Model model) {
        Shebei shebei = shebeiService.findById(androidId);
        model.addAttribute("shebei", shebei);
        return "updateEmp";
    }

    /**
     * 更新设备信息
     */
    @RequestMapping("update")
    public String update(Shebei shebei) {
        Date date =new Date();
        shebei.setUpdateTime(date);
        shebeiService.update(shebei);
        return "redirect:/shebei/lists";
    }

    /**
     * 根据id删除信息
     */
    @RequestMapping("delete")
    public String delete(Integer androidId) {
        shebeiService.delete(androidId);
        return "redirect:/shebei/lists";//跳转到员工列表
    }
    /**
     * 添加设备信息
     */
    @RequestMapping("save")
    public String insert(Shebei shebei){
        Date date = new Date();
//        if(shebei != null && shebei.getAndroidId() != null){
//            //
//            shebei.setUpdateTime(date);
//        }else {
//            //新增
//            //2.保存员工信息
//            shebei.setCreateTime(date);
//            shebei.setUpdateTime(date);
//            shebeiService.save(shebei);
//        }

            shebei.setUpdateTime(date);
            shebei.setCreateTime(date);
            shebeiService.save(shebei);
        return "redirect:/shebei/lists";//保存成功跳转到列表页面
    }
    /**
     * 1.java实体类要用java.util.Date
     * 2.新增数据，要插入Createtime，Updatetime时间
     *      数据，要Updatetime时间
     * 3.列表格式化时间
     * 4.insert语句主键自增 useGeneratedKeys="true" keyProperty="androidId"
     *      此时设置useGeneratedKeys参数值为true，在执行添加记录之后可以获取到数据库自动生成的主键ID。
     * 5.删除要软删
     * 6.过程，a根据id查询数据
     *           b再作修改
     */
}
